home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / INTERNET / PGP / PGP263I / pgp263i / src / c / fileio < prev    next >
Encoding:
Text File  |  1997-05-23  |  44.2 KB  |  1,780 lines

  1. /*      fileio.c  - I/O routines for PGP.
  2.    PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  3.  
  4.    (c) Copyright 1990-1996 by Philip Zimmermann.  All rights reserved.
  5.    The author assumes no liability for damages resulting from the use
  6.    of this software, even if the damage results from defects in this
  7.    software.  No warranty is expressed or implied.
  8.  
  9.    Note that while most PGP source modules bear Philip Zimmermann's
  10.    copyright notice, many of them have been revised or entirely written
  11.    by contributors who frequently failed to put their names in their
  12.    code.  Code that has been incorporated into PGP from other authors
  13.    was either originally published in the public domain or is used with
  14.    permission from the various authors.
  15.  
  16.    PGP is available for free to the public under certain restrictions.
  17.    See the PGP User's Guide (included in the release package) for
  18.    important information about licensing, patent restrictions on
  19.    certain algorithms, trademarks, copyrights, and export controls.
  20.  
  21.    Modified 16 Apr 92 - HAJK
  22.    Mods for support of VAX/VMS file system
  23.  
  24.    Modified 17 Nov 92 - HAJK
  25.    Change to temp file stuff for VMS.
  26.  */
  27.  
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #ifdef UNIX
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #ifdef _BSD
  38. #include <sys/param.h>
  39. #endif
  40. extern int errno;
  41. #endif                /* UNIX */
  42. #ifdef VMS
  43. #include <file.h>
  44. #include <assert.h>
  45. #endif
  46. #include "random.h"
  47. #include "mpilib.h"
  48. #include "mpiio.h"
  49. #include "fileio.h"
  50. #include "language.h"
  51. #include "pgp.h"
  52. #include "exitpgp.h"
  53. #include "charset.h"
  54. #include "system.h"
  55. #if defined(MSDOS) || defined(OS2) || defined (WIN32)
  56. #include <io.h>
  57. #include <fcntl.h>
  58. #endif
  59. #ifdef MACTC5
  60. #include "crypto.h"     /* for get_header_info_from_file() */
  61. #include "AEStuff.h"
  62. #include "AppGlobals.h"
  63. #include "MacPGP.h"
  64. #include "Macutil2.h"
  65. #include "Macutil3.h"
  66. #define MULTIPLE_DOTS
  67. extern Boolean AEProcessing;
  68. pascal Boolean idleProc(EventRecord * eventIn, long *sleep, RgnHandle * mouseRgn);
  69. #endif
  70.  
  71. char *ck_dup_output(char *, boolean, boolean);
  72.  
  73. #ifndef F_OK
  74. #define F_OK    0
  75. #define X_OK    1
  76. #define W_OK    2
  77. #define R_OK    4
  78. #endif                /* !F_OK */
  79.  
  80. /*
  81.  * DIRSEPS is a string of possible directory-separation characters
  82.  * The first one is the preferred one, which goes in between
  83.  * PGPPATH and the file name if PGPPATH is not terminated with a
  84.  * directory separator.
  85.  */
  86.  
  87. #if defined(MSDOS) || defined(__MSDOS__) || defined(OS2) || defined (WIN32)
  88. static char const DIRSEPS[] = "\\/:";
  89. #define BSLASH
  90.  
  91. #elif defined(ATARI)
  92. static char const DIRSEPS[] = "\\/:";
  93. #define BSLASH
  94.  
  95. #elif defined(UNIX)
  96. static char const DIRSEPS[] = "/";
  97. #define MULTIPLE_DOTS
  98.  
  99. #elif defined(AMIGA)
  100. static char const DIRSEPS[] = "/:";
  101. #define MULTIPLE_DOTS
  102.  
  103. #elif defined(VMS)
  104. static char const DIRSEPS[] = "]:";
  105.  
  106. #elif defined(EBCDIC)
  107. static char const DIRSEPS[] = "(";     /* Any more? */
  108. #define MULTIPLE_DOTS
  109.  
  110. #elif defined(MACTC5)
  111. #define MULTIPLE_DOTS
  112. static char const DIRSEPS[] = ":";
  113.  
  114. /* gjm:
  115.  */
  116. #elif defined(RISC_OS)
  117. static char const DIRSEPS[] = ".:";
  118.  
  119. #else
  120. /* #error is not portable, this has the same effect */
  121. #include "Unknown OS"
  122. #endif
  123.  
  124. #ifdef __PUREC__
  125. #include <ext.h>
  126. int access(const char *name,int flag)
  127. {
  128. struct ffblk dummy;
  129.     return findfirst(name,&dummy,-1);
  130. }
  131. #endif
  132.  
  133. /* 1st character of temporary file extension */
  134. /* gjm:
  135.  * we can't use $ in RISC OS, because it's special.
  136.  * So we use '_' instead.
  137.  * And here's an rparen to cancel the one in the EBCDIC thing aboe: )
  138.  */
  139. #ifdef RISC_OS
  140. #define TMP_EXT '_'        /* extensions will be /_## */
  141. #else
  142. #define    TMP_EXT    '$'        /* extensions are '.$##' */
  143. #endif
  144.  
  145. /* gjm:
  146.  * Since RISC OS (almost uniquely) doesn't leave the '.' char
  147.  * free for extensions, we need to cope with many places where
  148.  * '.' is hard-wired into the code.
  149.  * Wherever 'EXT_CHR' appears belowm the original source had '.'.
  150.  * There is also one "printf" format string with a similar change.
  151.  */
  152. #ifdef RISC_OS
  153. #define EXT_CHR '/'
  154. #else
  155. #define EXT_CHR '.'
  156. #endif
  157.  
  158. /* The PGPPATH environment variable */
  159.  
  160. static char PGPPATH[] = "PGPPATH";
  161.  
  162. /* Disk buffers, used here and in crypto.c */
  163. byte textbuf[DISKBUFSIZE];
  164. static unsigned textbuf2[2 * DISKBUFSIZE / sizeof(unsigned)];
  165.  
  166. boolean file_exists(char *filename)
  167. /*      Returns TRUE iff file exists. */
  168. {
  169. #ifdef MACTC5
  170.     FILE *f;
  171.     /* open file f for read, in binary (not text) mode...*/
  172.     if ((f = fopen(filename,FOPRBIN)) == NULL)
  173.         return(FALSE);
  174.     fclose(f);
  175.     return(TRUE);
  176. #else
  177.     return access(filename, F_OK) == 0;
  178. #endif
  179. }                /* file_exists */
  180.  
  181. static boolean is_regular_file(char *filename)
  182. {
  183. #ifdef S_ISREG
  184.     struct stat st;
  185.     return stat(filename, &st) != -1 && S_ISREG(st.st_mode);
  186. #else
  187.     return TRUE;
  188. #endif
  189. }
  190.  
  191.  
  192. /*
  193.  * This wipes a file with pseudo-random data.  The purpose of this is to
  194.  * make sure no sensitive information is left on the disk.  The use
  195.  * of pseudo-random data is to defeat disk compression drivers (such as
  196.  * Stacker and dblspace) so that we are guaranteed that the entire file
  197.  * has been overwritten.
  198.  *
  199.  * Note that the file MUST be open for read/write.
  200.  *
  201.  * It may not work to eliminate everything from non-volatile storage
  202.  * if the OS you're using does its own paging or swapping.  Then
  203.  * it's an issue of how the OS's paging device is wiped, and you can
  204.  * only hope that the space will be reused within a few seconds.
  205.  *
  206.  * Also, some file systems (in particular, the Logging File System
  207.  * for Sprite) do not write new data in the same place as old data,
  208.  * defeating this wiping entirely.  Fortunately, such systems
  209.  * usually don't need a swap file, and for small temp files, they
  210.  * do write-behind, so if you create and delete a file fast enough,
  211.  * it never gets written to disk at all.
  212.  */
  213.  
  214. /*
  215.  * The data is randomly generated with the size of the file as a seed.
  216.  * The data should be random and not leak information.  If someone is
  217.  * examining deleted files, presumably they can reconstruct the file size,
  218.  * so that's not a secret.  H'm... this wiping algorithm makes it easy to,
  219.  * given a block of data, find the size of the file it came from
  220.  * and the offset of this block within it.  That in turn reveals
  221.  * something about the state of the disk's allocation tables when the
  222.  * file was used, possibly making it easier to find other files created
  223.  * at neaby times - such as plaintext files.  Is this acceptable?
  224.  */
  225.  
  226. /*
  227.  * Theory of operation: We use the additive congruential RNG
  228.  * r[i] = r[i-24] + r[i-55], from Knuth, Vol. 2.  This is fast
  229.  * and has a long enough period that there should be no repetitions
  230.  * in even a huge file.  It is seeded with r[-55] through r[-1]
  231.  * using another polynomial-based RNG.  We seed a linear feedback
  232.  * shift register (CRC generator) with the size of the swap file,
  233.  * and clock in 0 bits.  Each 32 bits, the value of the generator is
  234.  * taken as the next integer.  This is just to ensure a reasonably
  235.  * even mix of 1's and 0's in the initialization vector.
  236.  */
  237.  
  238. /*
  239.  * This is the CRC-32 polynomial, which should be okay for random
  240.  * number generation.
  241.  * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
  242.  * = 1 0000 0100 1100 0001 0001 1101 1011 0111
  243.  * = 0x04c11db7
  244.  */
  245. #define POLY 0x04c11db7
  246.  
  247. static void wipeout(FILE * f)
  248. {
  249.     unsigned *p1, *p2, *p3;
  250.     unsigned long len;
  251.     unsigned long t;
  252.     int i;
  253.  
  254.     /* Get the file size */
  255.     fseek(f, 0L, SEEK_END);
  256.     len = ftell(f);
  257. #ifdef MACTC5
  258.     len = len + 4096 - (len % 4096);
  259. #endif
  260.     rewind(f);
  261.  
  262.     /* Seed of first RNG.  Inverted to get more 1 bits */
  263.     t = ~len;
  264.  
  265.     /* Initialize first 55 words of buf with pseudo-random stuff */
  266.     p1 = (unsigned *) textbuf2 + 55;
  267.     do {
  268.     for (i = 0; i < 32; i++)
  269.         t = (t & 0x80000000) ? t << 1 ^ POLY : t << 1;
  270.     *--p1 = (unsigned) t;
  271.     } while (p1 > (unsigned *) textbuf2);
  272.  
  273.     while (len) {
  274.     /* Fill buffer with pseudo-random integers */
  275.  
  276.     p3 = (unsigned *) textbuf2 + 55;
  277.     p2 = (unsigned *) textbuf2 + 24;
  278.     p1 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p1);
  279.     do {
  280.         *--p1 = *--p2 + *--p3;
  281.     } while (p2 > (unsigned *) textbuf2);
  282.  
  283.     p2 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p1);
  284.     do {
  285.         *--p1 = *--p2 + *--p3;
  286.     } while (p3 > (unsigned *) textbuf2);
  287.  
  288.     p3 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p3);
  289.     do {
  290.         *--p1 = *--p2 + *--p3;
  291.     } while (p1 > (unsigned *) textbuf2);
  292.  
  293.     /* Write it out - yes, we're ignoring errors */
  294.     if (len > sizeof(textbuf2)) {
  295.         fwrite((char const *) textbuf2, sizeof(textbuf2), 1, f);
  296.         len -= sizeof(textbuf2);
  297. #ifdef MACTC5
  298.         mac_poll_for_break();
  299. #endif
  300.     } else {
  301.         fwrite((char const *) textbuf2, len, 1, f);
  302.         len = 0;
  303.     }
  304.     }
  305. }
  306.  
  307.  
  308. /*
  309.  * Completely overwrite and erase file, so that no sensitive
  310.  * information is left on the disk.
  311.  */
  312. int wipefile(char *filename)
  313. {
  314.     FILE *f;
  315.     /* open file f for read/write, in binary (not text) mode... */
  316.     if ((f = fopen(filename, FOPRWBIN)) == NULL)
  317.     return -1;        /* error - file can't be opened */
  318.     wipeout(f);
  319.     fclose(f);
  320.     return 0;            /* normal return */
  321. }                /* wipefile */
  322.  
  323. /*
  324.  * Returns the part of a filename after all directory specifiers.
  325.  */
  326. char *file_tail(char *filename)
  327. {
  328.     char *p;
  329.     char const *s = DIRSEPS;
  330.  
  331.     while (*s) {
  332.     p = strrchr(filename, *s);
  333.     if (p)
  334.         filename = p + 1;
  335.     s++;
  336.     }
  337.  
  338.     return filename;
  339. }
  340.  
  341.  
  342. /* return TRUE if extension matches the end of filename */
  343. boolean has_extension(char *filename, char *extension)
  344. {
  345.     int lf = strlen(filename);
  346.     int lx = strlen(extension);
  347.  
  348.     if (lf <= lx)
  349.     return FALSE;
  350.     return !strcmp(filename + lf - lx, extension);
  351. }
  352.  
  353. /* return TRUE if path is a filename created by tempfile() */
  354. /* Filename matches "*.$[0-9][0-9]" */
  355. boolean is_tempfile(char *path)
  356. {
  357.     char *p = strrchr(path, EXT_CHR);    /* gjm: EXT_CHR was '.' */
  358.  
  359.     return p != NULL && p[1] == TMP_EXT &&
  360.     isdigit(p[2]) && isdigit(p[3]) && p[4] == '\0';
  361. }
  362.  
  363. /*
  364.  * Returns TRUE if user left off file extension, allowing default.
  365.  * Note that the name is misleading if multiple dots are allowed.
  366.  * not_pgp_extension or something would be better.
  367.  */
  368. boolean no_extension(char *filename)
  369. {
  370. #ifdef MULTIPLE_DOTS        /* filename can have more than one dot */
  371.     if (has_extension(filename, ASC_EXTENSION) ||
  372.     has_extension(filename, PGP_EXTENSION) ||
  373.     has_extension(filename, SIG_EXTENSION) ||
  374. #ifdef MACTC5
  375.         has_extension(filename,".tmp") ||
  376. #endif
  377.     is_tempfile(filename))
  378.     return FALSE;
  379.     else
  380.     return TRUE;
  381. #else
  382.     filename = file_tail(filename);
  383.  
  384.     return strrchr(filename, EXT_CHR) == NULL;    /* gjm: EXT_CHR was '.' */
  385. #endif
  386. }                /* no_extension */
  387.  
  388.  
  389. /* deletes trailing ".xxx" file extension after the period. */
  390. void drop_extension(char *filename)
  391. {
  392.     if (!no_extension(filename))
  393.     *strrchr(filename, EXT_CHR) = '\0';    /* gjm: EXT_CHR was '.' */
  394. }                /* drop_extension */
  395.  
  396.  
  397. /* append filename extension if there isn't one already. */
  398. void default_extension(char *filename, char *extension)
  399. {
  400.     if (no_extension(filename))
  401.     strcat(filename, extension);
  402. }                /* default_extension */
  403.  
  404. #ifndef MAX_NAMELEN
  405. #if defined(AMIGA) || defined(NeXT) || (defined(BSD) && BSD > 41) || (defined(sun) && defined(i386))
  406. #define    MAX_NAMELEN    255
  407. #else
  408. #ifdef MACTC5
  409. #define MAX_NAMELEN 31
  410. #else
  411. #include <limits.h>
  412. #endif
  413. #endif
  414. #endif
  415.  
  416. /* gjm:
  417.  * The following is not a no-op for RISC OS systems,
  418.  * despite the first comment inside the routine.
  419.  */
  420. /* truncate the filename so that an extension can be tacked on. */
  421. static void truncate_name(char *path, int ext_len)
  422. {
  423. #ifdef UNIX            /* for other systems this is a no-op */
  424.     char *p;
  425. #ifdef MAX_NAMELEN        /* overrides the use of pathconf() */
  426.     int namemax = MAX_NAMELEN;
  427. #else
  428.     int namemax;
  429. #ifdef _PC_NAME_MAX
  430.     char dir[MAX_PATH];
  431.  
  432.     strcpy(dir, path);
  433.     if ((p = strrchr(dir, '/')) == NULL) {
  434.     strcpy(dir, ".");
  435.     } else {
  436.     if (p == dir)
  437.         ++p;
  438.     *p = '\0';
  439.     }
  440.     if ((namemax = pathconf(dir, _PC_NAME_MAX)) <= ext_len)
  441.     return;
  442. #else
  443. #ifdef NAME_MAX
  444.     namemax = NAME_MAX;
  445. #else
  446.     namemax = 14;
  447. #endif                /* NAME_MAX */
  448. #endif                /* _PC_NAME_MAX */
  449. #endif                /* MAX_NAMELEN */
  450.  
  451.     if ((p = strrchr(path, '/')) == NULL)
  452.     p = path;
  453.     else
  454.     ++p;
  455.     if (strlen(p) > namemax - ext_len) {
  456.     if (verbose)
  457.         fprintf(pgpout, "Truncating filename '%s' ", path);
  458.     p[namemax - ext_len] = '\0';
  459.     if (verbose)
  460.         fprintf(pgpout, "to '%s'\n", path);
  461.     }
  462. #else
  463. #ifdef MACTC5
  464.     char *p;
  465.     p = file_tail(path);
  466.     if (verbose)
  467.         fprintf(pgpout, LANG("Truncating filename '%s' "), path);
  468.     if (strlen(p) + ext_len > MAX_NAMELEN) p[MAX_NAMELEN - ext_len] = '\0';
  469. #endif  /* MACTC5 */
  470. /* gjm:
  471.  */
  472. #ifdef RISC_OS
  473.     char *p,*q;
  474.     char all[MAX_PATH];
  475.     strcpy(all,path);
  476.     p=strrchr(all,'.'); q=strrchr(all,':');
  477.     if (p) { if (q && q-p>0) p=q+1; else ++p; }
  478.       else { if (q)          p=q+1; else p=all; }
  479.     if (strlen(p) > gjm_maxlen-ext_len) {
  480.       if (verbose) fprintf(pgpout,"Truncating filename '%s' ",all);
  481.       p[gjm_maxlen-ext_len]='\0';
  482.       if (verbose) fprintf(pgpout,"to '%s'\n",all);
  483.     }
  484. #endif
  485. #endif                /* UNIX */
  486. }
  487.  
  488. /* change the filename extension. */
  489. void force_extension(char *filename, char *extension)
  490. {
  491.     drop_extension(filename);    /* out with the old */
  492.     truncate_name(filename, strlen(extension));
  493.     strcat(filename, extension);    /* in with the new */
  494. }                /* force_extension */
  495.  
  496.  
  497. /*
  498.  * Get yes/no answer from user, returns TRUE for yes, FALSE for no.
  499.  * First the translations are checked, if they don't match 'y' and 'n'
  500.  * are tried.
  501.  */
  502. #ifdef MACTC5
  503.  
  504. boolean getyesno(char default_answer)
  505. {
  506.   extern FILE *logfile;
  507.   short  alertid,i,large,err;
  508.   char dfault[8], ndfault[8];
  509.   ProcessSerialNumber psn;
  510.   if (batchmode)
  511.       return(default_answer == 'y' ? TRUE : FALSE);
  512.   if (strlen(Yes_No_Message)<72) large=0;
  513.   else large=100;
  514.   strcpy(dfault,default_answer == 'y' ? LANG("y") : LANG("n"));
  515.   strcpy(ndfault,default_answer == 'n' ? LANG("y") : LANG("n"));
  516.   for(i=0;i<strlen(Yes_No_Message);i++)
  517.   if (Yes_No_Message[i]<' ' && Yes_No_Message[i]>=0) Yes_No_Message[i]=' ';    /* It's a signed char! */
  518.   InitCursor();
  519.   alertid=(default_answer == 'n' ? 211+large : 212+large);
  520.   c2pstr(Yes_No_Message);
  521.   ParamText((uchar *)Yes_No_Message,(uchar *)"", \
  522.           (uchar *)"",(uchar *)"");
  523.   if (AEProcessing) {
  524.       if (gHasProcessManager)
  525.           GetFrontProcess(&psn);
  526.       if(MyInteractWithUser())
  527.           return default_answer;
  528.       if (gHasProcessManager)
  529.         SetFrontProcess(&psn);
  530.   }
  531.   if (CautionAlert(alertid,nil)==1){
  532.    p2cstr((uchar *)Yes_No_Message);
  533.    fputs(strcat(Yes_No_Message,dfault),stderr);
  534.    fputc('\n',stderr);
  535.    fflush(stderr);
  536.    return(default_answer == 'y' ? TRUE : FALSE);
  537.    }
  538.   p2cstr((uchar *)Yes_No_Message);
  539.   fputs(strcat(Yes_No_Message,ndfault),stderr);
  540.   fputc('\n',stderr);
  541.   fflush(stderr);
  542.   return(default_answer == 'n' ? TRUE : FALSE);
  543.   }
  544. #else
  545.  
  546. boolean getyesno(char default_answer)
  547. {
  548.     char buf[8];
  549.     static char yes[8], no[8];
  550.  
  551.     if (yes[0] == '\0') {
  552.     strncpy(yes, LANG("y"), 7);
  553.     strncpy(no, LANG("n"), 7);
  554.     }
  555.     if (!batchmode) {        /* return default answer in batchmode */
  556.     getstring(buf, 6, TRUE);    /* echo keyboard input */
  557.     strlwr(buf);
  558.     if (!strncmp(buf, no, strlen(no)))
  559.         return FALSE;
  560.     if (!strncmp(buf, yes, strlen(yes)))
  561.         return TRUE;
  562.     if (buf[0] == 'n')
  563.         return FALSE;
  564.     if (buf[0] == 'y')
  565.         return TRUE;
  566.     }
  567.     return default_answer == 'y' ? TRUE : FALSE;
  568. }                /* getyesno */
  569. #endif
  570.  
  571. /* if user consents to it, change the filename extension. */
  572. char *maybe_force_extension(char *filename, char *extension)
  573. {
  574.     static char newname[MAX_PATH];
  575.     if (!has_extension(filename, extension)) {
  576.     strcpy(newname, filename);
  577.     force_extension(newname, extension);
  578.     if (!file_exists(newname)) {
  579.         fprintf(pgpout, LANG("\nShould '%s' be renamed to '%s' (Y/n)? "),
  580.             filename, newname);
  581.         if (getyesno('y'))
  582.         return newname;
  583.     }
  584.     }
  585.     return NULL;
  586. }                /* maybe_force_extension */
  587.  
  588. /*
  589.  * Add a trailing directory separator to a name, if absent.
  590.  */
  591. static void addslash(char *name)
  592. {
  593.     int i = strlen(name);
  594.  
  595.     if (i != 0 && !strchr(DIRSEPS, name[i - 1])) {
  596.     name[i] = DIRSEPS[0];
  597.     name[i + 1] = '\0';
  598.     }
  599. }
  600.  
  601. /*
  602.  * Builds a filename with a complete path specifier from the environmental
  603.  * variable PGPPATH.
  604.  */
  605. char *buildfilename(char *result, char *fname)
  606. {
  607. #ifdef MACTC5
  608.     char const *s;
  609. #else
  610.     char const *s = getenv(PGPPATH);
  611. #endif
  612.     result[0] = '\0';
  613. #ifdef MACTC5
  614.     return(strcpy(result,fname));
  615. #endif
  616.  
  617.     if (s && strlen(s) <= 50) {
  618.     strcpy(result, s);
  619.     }
  620. #ifdef UNIX
  621.     /* On Unix, default to $HOME/.pgp, otherwise, current directory. */
  622.     else {
  623.     s = getenv("HOME");
  624.     if (s && strlen(s) <= 50) {
  625.         strcpy(result, s);
  626.         addslash(result);
  627.         strcat(result, ".pgp");
  628.     }
  629.     }
  630. #endif                /* UNIX */
  631.  
  632.     addslash(result);
  633.     strcat(result, fname);
  634.     return result;
  635. }                /* buildfilename */
  636.  
  637. char *buildsysfilename(char *result, char *fname)
  638. {
  639. #ifdef PGP_SYSTEM_DIR
  640.     strcpy(result, PGP_SYSTEM_DIR);
  641.     strcat(result, fname);
  642.     if (file_exists(result))
  643.     return result;
  644. #endif
  645.     buildfilename(result, fname);    /* Put name back for error */
  646.     return result;
  647. }
  648.  
  649.  
  650. /* Convert filename to canonical form, with slashes as separators */
  651. void file_to_canon(char *filename)
  652. {
  653. #ifdef EBCDIC
  654.     CONVERT_TO_CANONICAL_CHARSET(filename);
  655. #endif
  656. #ifdef BSLASH
  657.     while (*filename) {
  658.     if (*filename == '\\')
  659.         *filename = '/';
  660.     ++filename;
  661.     }
  662. #else    /* 203a */
  663. #ifdef MACTC5
  664.     while (*filename) {
  665.         if (*filename == ':')
  666.             *filename = '/';
  667.         ++filename;
  668.         }
  669. #endif
  670. #endif
  671. }
  672.  
  673. #ifdef EBCDIC
  674. /* Convert filename from canonical form */
  675. void file_from_canon(char *filename)
  676. {
  677.    strcpy( filename, LOCAL_CHARSET(filename) );
  678. }
  679. #endif /* EBCDIC */
  680.  
  681.  
  682. int write_error(FILE * f)
  683. {
  684.     fflush(f);
  685.     if (ferror(f)) {
  686. #ifdef ENOSPC
  687.     if (errno == ENOSPC)
  688.         fprintf(pgpout, LANG("\nDisk full.\n"));
  689.     else
  690. #endif
  691.         fprintf(pgpout, LANG("\nFile write error.\n"));
  692.     return -1;
  693.     }
  694.     return 0;
  695. }
  696.  
  697. /* copy file f to file g, for longcount bytes */
  698. int copyfile(FILE * f, FILE * g, word32 longcount)
  699. {
  700.     int count, status = 0;
  701.     do {            /* read and write the whole file... */
  702.     if (longcount < (word32) DISKBUFSIZE)
  703.         count = (int) longcount;
  704.     else
  705.         count = DISKBUFSIZE;
  706.     count = fread(textbuf, 1, count, f);
  707.     if (count > 0) {
  708.         if (CONVERSION != NO_CONV) {
  709.         int i;
  710.         for (i = 0; i < count; i++)
  711.             textbuf[i] = (CONVERSION == EXT_CONV) ?
  712.             EXT_C(textbuf[i]) :
  713.             INT_C(textbuf[i]);
  714.         }
  715.         if (fwrite(textbuf, 1, count, g) != count) {
  716.         /* Problem: return error value */
  717.         status = -1;
  718.         break;
  719.         }
  720.         longcount -= count;
  721. #ifdef MACTC5
  722.             mac_poll_for_break();
  723. #endif
  724.     }
  725.     /* if text block was short, exit loop */
  726.     } while (count == DISKBUFSIZE);
  727.     burn(textbuf);        /* burn sensitive data on stack */
  728.     return status;
  729. }                /* copyfile */
  730.  
  731. /*
  732.  * Like copyfile, but takes a position for file f.  Returns with
  733.  * f and g pointing just past the copied data.
  734.  */
  735. int copyfilepos(FILE * f, FILE * g, word32 longcount, word32 fpos)
  736. {
  737.     fseek(f, fpos, SEEK_SET);
  738.     return copyfile(f, g, longcount);
  739. }
  740.  
  741.  
  742. /* copy file f to file g, for longcount bytes.  Convert to
  743.  * canonical form as we go.  f is open in text mode.  Canonical
  744.  * form uses crlf's as line separators.
  745.  */
  746. int copyfile_to_canon(FILE * f, FILE * g, word32 longcount)
  747. {
  748.     int count, status = 0;
  749.     byte c, *tb1, *tb2;
  750.     int i, nbytes;
  751.     int nspaces = 0;
  752. #ifdef MACTC5
  753.     Boolean warning = true; /* MACTC5 */
  754. #endif
  755.     do {            /* read and write the whole file... */
  756.     if (longcount < (word32) DISKBUFSIZE)
  757.         count = (int) longcount;
  758.     else
  759.         count = DISKBUFSIZE;
  760.     count = fread(textbuf, 1, count, f);
  761.     if (count > 0) {
  762.         /* Convert by adding CR before LF */
  763.         tb1 = textbuf;
  764.         tb2 = (byte *) textbuf2;
  765.         for (i = 0; i < count; ++i) {
  766.         switch (CONVERSION) {
  767.         case EXT_CONV:
  768.             c = EXT_C(*tb1++);
  769.             break;
  770.         case INT_CONV:
  771.             c = INT_C(*tb1++);
  772.             break;
  773.         default:
  774.             c = *tb1++;
  775.         }
  776. #ifdef MACTC5
  777.         if ( (((uchar) c) < ' ') && (c != '\n') && (c != '\t') && warning) {
  778.             warning = false;
  779.             fprintf(stdout, "\aWarning text file contains control characters!\n");
  780.         }
  781. #endif
  782.         if (strip_spaces) {
  783.             if (c == ' ') {
  784.             /* Don't output spaces yet */
  785.             nspaces += 1;
  786.             } else {
  787.             if (c == '\n') {
  788.                 *tb2++ = '\r';
  789.                 nspaces = 0;    /* Delete trailing spaces */
  790.             }
  791.             if (nspaces) {
  792.                 /* Put out spaces now */
  793.                 do
  794.                 *tb2++ = ' ';
  795.                 while (--nspaces);
  796.             }
  797.             *tb2++ = c;
  798.             }
  799.         } else {
  800.             if (c == '\n')
  801.             *tb2++ = '\r';
  802.             *tb2++ = c;
  803.         }
  804.         }
  805.         nbytes = tb2 - (byte *) textbuf2;
  806.         if (fwrite(textbuf2, 1, nbytes, g) != nbytes) {
  807.         /* Problem: return error value */
  808.         status = -1;
  809.         break;
  810.         }
  811.         longcount -= count;
  812.     }
  813.     /* if text block was short, exit loop */
  814.     } while (count == DISKBUFSIZE);
  815.     burn(textbuf);        /* burn sensitive data on stack */
  816.     burn(textbuf2);
  817.     return status;
  818. }                /* copyfile_to_canon */
  819.  
  820.  
  821. /* copy file f to file g, for longcount bytes.  Convert from
  822.  * canonical to local form as we go.  g is open in text mode.  Canonical
  823.  * form uses crlf's as line separators.
  824.  */
  825. int copyfile_from_canon(FILE * f, FILE * g, word32 longcount)
  826. {
  827.     int count, status = 0;
  828.     byte c, *tb1, *tb2;
  829.     int i, nbytes;
  830.     do {            /* read and write the whole file... */
  831.     if (longcount < (word32) DISKBUFSIZE)
  832.         count = (int) longcount;
  833.     else
  834.         count = DISKBUFSIZE;
  835.     count = fread(textbuf, 1, count, f);
  836.     if (count > 0) {
  837.         /* Convert by removing CR's */
  838.         tb1 = textbuf;
  839.         tb2 = (byte *) textbuf2;
  840.         for (i = 0; i < count; ++i) {
  841.         switch (CONVERSION) {
  842.         case EXT_CONV:
  843.             c = EXT_C(*tb1++);
  844.             break;
  845.         case INT_CONV:
  846.             c = INT_C(*tb1++);
  847.             break;
  848.         default:
  849.             c = *tb1++;
  850.         }
  851.         if (c != '\r')
  852.             *tb2++ = c;
  853.         }
  854.         nbytes = tb2 - (byte *) textbuf2;
  855.         if (fwrite(textbuf2, 1, nbytes, g) != nbytes) {
  856.         /* Problem: return error value */
  857.         status = -1;
  858.         break;
  859.         }
  860.         longcount -= count;
  861.     }
  862.     /* if text block was short, exit loop */
  863.     } while (count == DISKBUFSIZE);
  864.     burn(textbuf);        /* burn sensitive data on stack */
  865.     burn(textbuf2);
  866.     return status;
  867. }                /* copyfile_from_canon */
  868.  
  869. /*      Copy srcFile to destFile  */
  870. int copyfiles_by_name(char *srcFile, char *destFile)
  871. {
  872.     FILE *f, *g;
  873.     int status = 0;
  874.     long fileLength;
  875.  
  876.     f = fopen(srcFile, FOPRBIN);
  877.     if (f == NULL)
  878.     return -1;
  879.     g = fopen(destFile, FOPWBIN);
  880.     if (g == NULL) {
  881.     fclose(f);
  882.     return -1;
  883.     }
  884.     /* Get file length and copy it */
  885.     fseek(f, 0L, SEEK_END);
  886.     fileLength = ftell(f);
  887.     rewind(f);
  888.     status = copyfile(f, g, fileLength);
  889.     fclose(f);
  890.     if (write_error(g))
  891.     status = -1;
  892.     fclose(g);
  893.     return status;
  894. }                /* copyfiles_by_name */
  895.  
  896. /* Copy srcFile to destFile, converting to canonical text form  */
  897. int make_canonical(char *srcFile, char *destFile)
  898. {
  899.     FILE *f, *g;
  900.     int status = 0;
  901.     long fileLength;
  902.  
  903.     if (((f = fopen(srcFile, FOPRTXT)) == NULL) ||
  904.     ((g = fopen(destFile, FOPWBIN)) == NULL))
  905.     /* Can't open files */
  906.     return -1;
  907.  
  908.     /* Get file length and copy it */
  909.     fseek(f, 0L, SEEK_END);
  910.     fileLength = ftell(f);
  911.     rewind(f);
  912.     CONVERSION = INT_CONV;
  913.     status = copyfile_to_canon(f, g, fileLength);
  914.     CONVERSION = NO_CONV;
  915.     fclose(f);
  916.     if (write_error(g))
  917.     status = -1;
  918.     fclose(g);
  919.     return status;
  920. }                /* make_canonical */
  921.  
  922. /*
  923.  * Like rename() but will try to copy the file if the rename fails.
  924.  * This is because under OS's with multiple physical volumes if the
  925.  * source and destination are on different volumes the rename will fail
  926.  */
  927. int rename2(char *srcFile, char *destFile)
  928. {
  929.     FILE *f, *g;
  930. #ifdef MACTC5
  931.     int copy=-1;
  932. #endif
  933.     int status = 0;
  934.     long fileLength;
  935.  
  936. #ifdef MACTC5
  937.     copy=MoveRename(srcFile,destFile);
  938. if (copy)
  939. #else
  940. /* gjm:
  941.  * RISC OS, evidently like VMS and MVS, doesn't guarantee
  942.  * to return -1 for errors in |rename()|.
  943.  */
  944. #if defined(VMS) || defined(C370) || defined(RISC_OS)
  945.     if (rename(srcFile, destFile) != 0)
  946. #else
  947.     if (rename(srcFile, destFile) == -1)
  948. #endif
  949. #endif
  950.     {
  951.     /* Rename failed, try a copy */
  952.     if (((f = fopen(srcFile, FOPRBIN)) == NULL) ||
  953.         ((g = fopen(destFile, FOPWBIN)) == NULL))
  954.         /* Can't open files */
  955.         return -1;
  956.  
  957. #ifdef MACTC5
  958.         {
  959.         FInfo finderInfo;
  960.         c2pstr(srcFile);
  961.         c2pstr(destFile);
  962.         if(GetFInfo((uchar *)srcFile,0,&finderInfo)==0)
  963.             SetFInfo((uchar *)destFile,0,&finderInfo);
  964.         p2cstr((uchar *)srcFile);
  965.         p2cstr((uchar *)destFile);
  966.         }
  967. #endif
  968.  
  969.     /* Get file length and copy it */
  970.     fseek(f, 0L, SEEK_END);
  971.     fileLength = ftell(f);
  972.     rewind(f);
  973.     status = copyfile(f, g, fileLength);
  974.     if (write_error(g))
  975.         status = -1;
  976.  
  977.     /* Zap source file if the copy went OK, otherwise zap the (possibly
  978.        incomplete) destination file */
  979.     if (status >= 0) {
  980.         wipeout(f);        /* Zap source file */
  981.         fclose(f);
  982.         remove(srcFile);
  983.         fclose(g);
  984.     } else {
  985.         if (is_regular_file(destFile)) {
  986.         wipeout(g);    /* Zap destination file */
  987.         fclose(g);
  988.         remove(destFile);
  989.         } else {
  990.         fclose(g);
  991.         }
  992.         fclose(f);
  993.     }
  994.     }
  995.     return status;
  996. }
  997.  
  998. /* read the data from stdin to the phantom input file */
  999. int readPhantomInput(char *filename)
  1000. {
  1001.     FILE *outFilePtr;
  1002.     byte buffer[512];
  1003.     int bytesRead, status = 0;
  1004.  
  1005.     if (verbose)
  1006.     fprintf(pgpout, "writing stdin to file %s\n", filename);
  1007.     if ((outFilePtr = fopen(filename, FOPWBIN)) == NULL)
  1008.     return -1;
  1009.  
  1010. #if defined(MSDOS) || defined(OS2) || defined (WIN32)
  1011.     /* Under DOS must set input stream to binary mode to avoid data mangling */
  1012.     setmode(fileno(stdin), O_BINARY);
  1013. #endif                /* MSDOS || OS2 */
  1014.     while ((bytesRead = fread(buffer, 1, 512, stdin)) > 0)
  1015.     if (fwrite(buffer, 1, bytesRead, outFilePtr) != bytesRead) {
  1016.         status = -1;
  1017.         break;
  1018.     }
  1019.     if (write_error(outFilePtr))
  1020.     status = -1;
  1021.     fclose(outFilePtr);
  1022. #if defined(MSDOS) || defined(OS2) || defined (WIN32)
  1023.     setmode(fileno(stdin), O_TEXT);    /* Reset stream */
  1024. #endif                /* MSDOS || OS2 */
  1025.     return status;
  1026. }
  1027.  
  1028. /* write the data from the phantom output file to stdout */
  1029. int writePhantomOutput(char *filename)
  1030. {
  1031.     FILE *outFilePtr;
  1032.     byte buffer[512];
  1033.     int bytesRead, status = 0;
  1034.  
  1035.     if (verbose)
  1036.     fprintf(pgpout, "writing file %s to stdout\n", filename);
  1037.     /* this can't fail since we just created the file */
  1038.     outFilePtr = fopen(filename, FOPRBIN);
  1039.  
  1040. #if defined(MSDOS) || defined(OS2) || defined (WIN32)
  1041.     setmode(fileno(stdout), O_BINARY);
  1042. #endif                /* MSDOS || OS2 */
  1043.     while ((bytesRead = fread(buffer, 1, 512, outFilePtr)) > 0)
  1044.     if (fwrite(buffer, 1, bytesRead, stdout) != bytesRead) {
  1045.         status = -1;
  1046.         break;
  1047.     }
  1048.     fclose(outFilePtr);
  1049.     fflush(stdout);
  1050.     if (ferror(stdout)) {
  1051.     status = -1;
  1052.     fprintf(pgpout, LANG("\007Write error on stdout.\n"));
  1053.     }
  1054. #if defined(MSDOS) || defined(OS2) || defined (WIN32)
  1055.     setmode(fileno(stdout), O_TEXT);
  1056. #endif                /* MSDOS || OS2 */
  1057.  
  1058.     return status;
  1059. }
  1060.  
  1061. /* Return the size from the current position of file f to the end */
  1062. word32 fsize(FILE * f)
  1063. {
  1064.     long fpos = ftell(f);
  1065.     long fpos2;
  1066.  
  1067.     fseek(f, 0L, SEEK_END);
  1068.     fpos2 = ftell(f);
  1069.     fseek(f, fpos, SEEK_SET);
  1070.     return (word32) (fpos2 - fpos);
  1071. }
  1072.  
  1073. /* Return TRUE if file filename looks like a pure text file */
  1074. int is_text_file (char *filename) /* EWS */
  1075. {
  1076.     FILE *f = fopen(filename,"r");      /* FOPRBIN gives problem with VMS */
  1077.     int i, n, lfctr = 0;
  1078.     unsigned char buf[512];
  1079.     unsigned char *bufptr = buf;
  1080.     unsigned char c;
  1081.  
  1082.     if (!f)
  1083.         return FALSE;          /* error opening it, so not a text file */
  1084.     i = n = fread (buf, 1, sizeof(buf), f);
  1085.     fclose(f);
  1086.     if (n <= 0)
  1087.         return FALSE;          /* empty file or error, not a text file */
  1088.     if (compressSignature(buf) >= 0)
  1089.         return FALSE;
  1090.     while (i--) {
  1091.         c = *bufptr++;
  1092.         if (c == '\n' || c == '\r')
  1093.             lfctr=0;
  1094.         else /* allow BEL BS HT LF VT FF CR EOF ESC control characters */
  1095.         {
  1096. #ifdef EBCDIC
  1097.         if (iscntrl(c) && c!=BEL && c!=BS && c!=HT && c!=LF && c!=VT && c!=FF && c!=CR && c!=EOF && c!=ESC)
  1098. #else
  1099.             if (c < '\007' || (c > '\r' && c < ' ' && c != '\032' && c != '\033'))
  1100. #endif
  1101.                 return FALSE;  /* not a text file */
  1102.             lfctr++;
  1103. /*          if (lfctr>132) return FALSE; /* line too long. Not a text file */
  1104.         }
  1105.     }
  1106.     return TRUE;
  1107. }                               /* is_text_file */
  1108.  
  1109. VOID *xmalloc(unsigned size)
  1110. {
  1111.     VOID *p;
  1112.     if (size == 0)
  1113.     ++size;
  1114.     p = malloc(size);
  1115.     if (p == NULL) {
  1116.     fprintf(stderr, LANG("\n\007Out of memory.\n"));
  1117.     exitPGP(1);
  1118.     }
  1119.     return p;
  1120. }
  1121.  
  1122. /*----------------------------------------------------------------------
  1123.  *    temporary file routines
  1124.  */
  1125.  
  1126.  
  1127. #define MAXTMPF 8
  1128.  
  1129. #define    TMP_INUSE    2
  1130.  
  1131. static struct {
  1132.     char path[MAX_PATH];
  1133.     int flags;
  1134.     int num;
  1135. } tmpf[MAXTMPF];
  1136.  
  1137. static char tmpdir[256];    /* temporary file directory */
  1138. static char outdir[256];    /* output directory */
  1139. static char tmpbasename[64] = "pgptemp";    /* basename for
  1140.                            temporary files */
  1141.  
  1142.  
  1143. /*
  1144.  * set directory for temporary files.  path will be stored in
  1145.  * tmpdir[] with an appropriate trailing path separator.
  1146.  */
  1147. void settmpdir(char *path)
  1148. {
  1149.     char *p;
  1150.  
  1151.     if (path == NULL || *path == '\0') {
  1152.     tmpdir[0] = '\0';
  1153.     return;
  1154.     }
  1155.     strcpy(tmpdir, path);
  1156.     p = tmpdir + strlen(tmpdir) - 1;
  1157. #ifdef MACTC5
  1158.     if (*p != '/' && *p != '\\' && *p != ']' && *p != ':')
  1159.     {    /* append path separator, either / or \ */
  1160.         if ((p = strchr(tmpdir, '/')) == NULL &&
  1161.             (p = strchr(tmpdir, '\\')) == NULL &&
  1162.             (p = strchr(tmpdir, ':')) == NULL)
  1163.             p = ":";    /* path did not contain / or \ or :, use : */
  1164.         strncat(tmpdir, p, 1);
  1165. /* gjm:
  1166.  * our path separator is '.', not '/' or '\'
  1167.  */
  1168. #elif defined(RISC_OS)
  1169.     if (*p != '.' && *p !=':') strcat(tmpdir,".");
  1170. #else
  1171.     if (*p != '/' && *p != '\\' && *p != ']' && *p != ':') {
  1172.     /* append path separator, either / or \ */
  1173.     if ((p = strchr(tmpdir, '/')) == NULL &&
  1174.         (p = strchr(tmpdir, '\\')) == NULL)
  1175.         p = "/";        /* path did not contain / or \, use / */
  1176.     strncat(tmpdir, p, 1);
  1177.     }
  1178. #endif
  1179. }
  1180.  
  1181. /*
  1182.  * set output directory to avoid a file copy when temp file is renamed to
  1183.  * output file.  the argument filename must be a valid path for a file, not
  1184.  * a directory.
  1185.  */
  1186. void setoutdir(char *filename)
  1187. {
  1188.     char *p;
  1189.  
  1190.     if (filename == NULL) {
  1191.     strcpy(outdir, tmpdir);
  1192.     return;
  1193.     }
  1194.     strcpy(outdir, filename);
  1195.     p = file_tail(outdir);
  1196.     strcpy(tmpbasename, p);
  1197.     *p = '\0';
  1198.     drop_extension(tmpbasename);
  1199. #if !defined(BSD42) && !defined(BSD43) && !defined(sun)
  1200.     /*
  1201.      *  we don't depend on pathconf here, if it returns an incorrect value
  1202.      * for NAME_MAX (like Linux 0.97 with minix FS) finding a unique name
  1203.      * for temp files can fail.
  1204.      */
  1205.     tmpbasename[10] = '\0';    /* 14 char limit */
  1206. #endif
  1207. }
  1208.  
  1209. /*
  1210.  * return a unique temporary file name
  1211.  */
  1212. char *tempfile(int flags)
  1213. {
  1214.     int i, j;
  1215.     int num;
  1216.     int fd;
  1217. #ifndef UNIX
  1218.     FILE *fp;
  1219. #endif
  1220.  
  1221.     for (i = 0; i < MAXTMPF; ++i)
  1222.     if (tmpf[i].flags == 0)
  1223.         break;
  1224.  
  1225.     if (i == MAXTMPF) {
  1226.     /* message only for debugging, no need for LANG */
  1227.     fprintf(stderr, "\n\007Out of temporary files\n");
  1228.     return NULL;
  1229.     }
  1230.   again:
  1231.     num = 0;
  1232.     do {
  1233.     for (j = 0; j < MAXTMPF; ++j)
  1234.         if (tmpf[j].flags && tmpf[j].num == num)
  1235.         break;
  1236.     if (j < MAXTMPF)
  1237.         continue;        /* sequence number already in use */
  1238. /* gjm:
  1239.  * (1) there used to be a '.' here where now there's a '%c'
  1240.  *     and an EXT_CHR arg;
  1241.  * (2) under RISC OS we limit the length of the |tmpbasename| string.
  1242.  * Original first line was
  1243.  *      sprintf(tmpf[i].path, "%s%s.%c%02d",
  1244.  */
  1245. #ifdef RISC_OS
  1246.     sprintf(tmpf[i].path, "%s%.6s%c%c%02d",
  1247. #else
  1248.     sprintf(tmpf[i].path, "%s%s%c%c%02d",
  1249. #endif
  1250.         ((flags & TMP_TMPDIR) && *tmpdir ? tmpdir : outdir),
  1251.         tmpbasename, EXT_CHR/*gjm*/, TMP_EXT, num);
  1252.     if (!file_exists(tmpf[i].path))
  1253.         break;
  1254.     }
  1255.     while (++num < 100);
  1256.  
  1257.     if (num == 100) {
  1258.     fprintf(pgpout, "\n\007tempfile: cannot find unique name\n");
  1259.     return NULL;
  1260.     }
  1261. #if defined(UNIX) || defined(VMS)
  1262.     if ((fd = open(tmpf[i].path, O_EXCL | O_RDWR | O_CREAT, 0600)) != -1)
  1263.     close(fd);
  1264. #else
  1265.     if ((fp = fopen(tmpf[i].path, "w")) != NULL)
  1266.     fclose(fp);
  1267.     fd = (fp == NULL ? -1 : 0);
  1268. #endif
  1269.  
  1270.     if (fd == -1) {
  1271.     if (!(flags & TMP_TMPDIR)) {
  1272.         flags |= TMP_TMPDIR;
  1273.         goto again;
  1274.     }
  1275. #ifdef UNIX
  1276.     else if (tmpdir[0] == '\0') {
  1277.         strcpy(tmpdir, "/tmp/");
  1278.         goto again;
  1279.     }
  1280. /* gjm:
  1281.  * Just as on Unix we always try in /tmp,
  1282.  * so on RISC OS we always try in $.tmp .
  1283.  */
  1284. #elif defined(RISC_OS)
  1285.     else if (tmpdir[0] == '\0') {
  1286.         strcpy(tmpdir, "$.tmp.");
  1287.         goto again;
  1288.     }
  1289. #endif
  1290.     }
  1291.     if (fd == -1) {
  1292.     fprintf(pgpout, LANG("\n\007Cannot create temporary file '%s'\n"),
  1293.         tmpf[i].path);
  1294.     user_error();
  1295.     }
  1296. #if defined(VMS) || defined(C370)
  1297.     remove(tmpf[i].path);
  1298. #endif
  1299.  
  1300.     tmpf[i].num = num;
  1301.     tmpf[i].flags = flags | TMP_INUSE;
  1302.     if (verbose)
  1303.     fprintf(pgpout, "tempfile: created '%s'\n", tmpf[i].path);
  1304.     return tmpf[i].path;
  1305. }                /* tempfile */
  1306.  
  1307. /*
  1308.  * remove temporary file, wipe if necessary.
  1309.  */
  1310. void rmtemp(char *name)
  1311. {
  1312.     int i;
  1313.  
  1314.     for (i = 0; i < MAXTMPF; ++i)
  1315.     if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
  1316.         break;
  1317.  
  1318.     if (i < MAXTMPF) {
  1319.     if (strlen(name) > 3 && name[strlen(name) - 3] == TMP_EXT) {
  1320.         /* only remove file if name hasn't changed */
  1321.         if (verbose)
  1322.         fprintf(pgpout, "rmtemp: removing '%s'\n", name);
  1323.         if (tmpf[i].flags & TMP_WIPE)
  1324.         wipefile(name);
  1325.         if (!remove(name)) {
  1326.         tmpf[i].flags = 0;
  1327.         } else if (verbose) {
  1328.         fprintf(stderr, "\nrmtemp: Failed to remove %s", name);
  1329.         perror("\nError");
  1330.         }
  1331.     } else if (verbose)
  1332.         fprintf(pgpout, "rmtemp: not removing '%s'\n", name);
  1333.     }
  1334. }                /* rmtemp */
  1335.  
  1336. /*
  1337.  * make temporary file permanent, returns the new name.
  1338.  */
  1339. char *savetemp(char *name, char *newname)
  1340. {
  1341.     int i, overwrite;
  1342.  
  1343.     if (strcmp(name, newname) == 0)
  1344.     return name;
  1345.  
  1346.     for (i = 0; i < MAXTMPF; ++i)
  1347.     if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
  1348.         break;
  1349.  
  1350.     if (i < MAXTMPF) {
  1351.     if (strlen(name) < 4 || name[strlen(name) - 3] != TMP_EXT) {
  1352.         if (verbose)
  1353.         fprintf(pgpout, "savetemp: not renaming '%s' to '%s'\n",
  1354.             name, newname);
  1355.         return name;    /* return original file name */
  1356.     }
  1357.     }
  1358.  
  1359.     newname = ck_dup_output(newname, FALSE, TRUE);
  1360.     if (newname==NULL)
  1361.         return(NULL);
  1362.  
  1363.     if (verbose)
  1364.     fprintf(pgpout, "savetemp: renaming '%s' to '%s'\n", name, newname);
  1365.     if (rename2(name, newname) < 0) {
  1366.     /* errorLvl = UNKNOWN_FILE_ERROR; */
  1367.     fprintf(pgpout, LANG("Can't create output file '%s'\n"), newname);
  1368.     return NULL;
  1369.     }
  1370.     if (i < MAXTMPF)
  1371.     tmpf[i].flags = 0;
  1372.     return newname;
  1373. } /* savetemp */
  1374.  
  1375. char *ck_dup_output(char *newname, boolean notest, boolean delete_dup)
  1376. {
  1377.     int overwrite;
  1378.     static char buf[MAX_PATH];
  1379.  
  1380.     while (file_exists(newname)) {
  1381.     if (batchmode && !force_flag) {
  1382.             fprintf(pgpout,LANG("\n\007Output file '%s' already exists.\n"),
  1383.                     newname);
  1384.         return NULL;
  1385.     }
  1386.     if (is_regular_file(newname)) {
  1387.         if (force_flag) {
  1388.         /* remove without asking */
  1389.         if (delete_dup) remove(newname);
  1390.         break;
  1391.         }
  1392.         fprintf(pgpout,
  1393.         LANG("\n\007Output file '%s' already exists.  Overwrite (y/N)? "),
  1394.          newname);
  1395.         overwrite = getyesno('n');
  1396.     } else {
  1397.         fprintf(pgpout,
  1398.             LANG("\n\007Output file '%s' already exists.\n"),newname);
  1399.         if (force_flag)    /* never remove special file */
  1400.         return NULL;
  1401.         overwrite = FALSE;
  1402.     }
  1403.  
  1404.     if (!overwrite) {
  1405.         fprintf(pgpout, "\n");
  1406.         fprintf(pgpout, LANG("Enter new file name:"));
  1407.         fprintf(pgpout, " ");
  1408. #ifdef MACTC5
  1409.             if (!GetFilePath(LANG("Enter new file name:"), buf, PUTFILE))
  1410.                 return(NULL);
  1411.             strcpy(newname, buf);
  1412.             fprintf(pgpout, "%s\n",buf);
  1413. #else
  1414.         getstring(buf, MAX_PATH - 1, TRUE);
  1415.         if (buf[0] == '\0')
  1416.         return(NULL);
  1417.         newname = buf;
  1418. #endif
  1419.     } else if (delete_dup)
  1420.             remove(newname);
  1421.         else
  1422.             break;
  1423.  
  1424.         if (notest) break;
  1425.     }
  1426.     return(newname);
  1427. } /* ck_dup_output */
  1428.  
  1429. /*
  1430.  * like savetemp(), only make backup of destname if it exists
  1431.  */
  1432. int savetempbak(char *tmpname, char *destname)
  1433. {
  1434.     char bakpath[MAX_PATH];
  1435. #ifdef MACTC5
  1436.     byte header[8];
  1437. #endif
  1438. #ifdef UNIX
  1439.     int mode = -1;
  1440. #endif
  1441.  
  1442.     if (is_tempfile(destname)) {
  1443.     remove(destname);
  1444.     } else {
  1445.     if (file_exists(destname)) {
  1446. #ifdef UNIX
  1447.         struct stat st;
  1448.         if (stat(destname, &st) != -1)
  1449.         mode = st.st_mode & 07777;
  1450. #endif
  1451.         strcpy(bakpath, destname);
  1452.         force_extension(bakpath, BAK_EXTENSION);
  1453.         remove(bakpath);
  1454. /* gjm:
  1455.  * again, RISC OS is like VMS and MVS in not guaranteeing -1 for failure.
  1456.  */
  1457. #if defined(VMS) || defined(C370) || defined(RISC_OS)
  1458.         if (rename(destname, bakpath) != 0)
  1459. #else
  1460.         if (rename(destname, bakpath) == -1)
  1461. #endif
  1462.         return -1;
  1463. #ifdef MACTC5
  1464.         get_header_info_from_file(bakpath, header, 8 );
  1465.         if (header[0] == CTB_CERT_SECKEY)
  1466.             PGPSetFinfo(bakpath,'SKey','MPGP');
  1467.         if (header[0] == CTB_CERT_PUBKEY)
  1468.             PGPSetFinfo(bakpath,'PKey','MPGP');
  1469. #endif
  1470.     }
  1471.     }
  1472.     if (savetemp(tmpname, destname) == NULL)
  1473.     return -1;
  1474. #if defined(UNIX)
  1475.     if (mode != -1)
  1476.     chmod(destname, mode);
  1477. #elif defined(MACTC5)
  1478.     get_header_info_from_file(destname, header, 8 );
  1479.     if (header[0] == CTB_CERT_SECKEY)
  1480.         PGPSetFinfo(destname,'SKey','MPGP');
  1481.     if (header[0] == CTB_CERT_PUBKEY)
  1482.         PGPSetFinfo(destname,'PKey','MPGP');
  1483. #endif
  1484.     return 0;
  1485. }
  1486.  
  1487. /*
  1488.  * remove all temporary files and wipe them if necessary
  1489.  */
  1490. void cleanup_tmpf(void)
  1491. {
  1492.     int i;
  1493.  
  1494.     for (i = 0; i < MAXTMPF; ++i)
  1495.     if (tmpf[i].flags)
  1496.         rmtemp(tmpf[i].path);
  1497. }                /* cleanup_tmpf */
  1498.  
  1499. #ifdef MACTC5
  1500. void mac_cleanup_tmpf(void)
  1501. {
  1502.     int i,err;
  1503.     HFileParam pb;
  1504.     char fname[256];
  1505.     for (i = 0; i < MAXTMPF; ++i)
  1506.         if (tmpf[i].flags)
  1507.            {
  1508.             strcpy(fname,tmpf[i].path);
  1509.             pb.ioCompletion=nil;
  1510.             c2pstr(fname);
  1511.             pb.ioNamePtr=(uchar *)fname;
  1512.             pb.ioVRefNum=0;
  1513.             pb.ioFDirIndex=0;
  1514.             pb.ioFRefNum=0;
  1515.             pb.ioDirID=0;
  1516.             err=PBHGetFInfo((HParmBlkPtr)&pb,false);
  1517.             if (pb.ioFRefNum!=0){
  1518.                 strcpy(fname,tmpf[i].path);
  1519.                 pb.ioCompletion=nil;
  1520.                 c2pstr(fname);
  1521.                 pb.ioNamePtr=(uchar *)fname;
  1522.                 pb.ioVRefNum=0;
  1523.                 pb.ioDirID=0;
  1524.                 err=PBClose((ParmBlkPtr)&pb,false);
  1525.                 }
  1526.             rmtemp(tmpf[i].path);
  1527.             }
  1528. }    /* mac_cleanup_tmpf */
  1529. #endif
  1530.  
  1531. /*
  1532.  * Routines to search for the manuals.
  1533.  *
  1534.  * Why all this code?
  1535.  *
  1536.  * Some people may object to PGP insisting on finding the manual somewhere
  1537.  * in the neighborhood to generate a key.  They bristle against this
  1538.  * seemingly authoritarian attitude.  Some people have even modified PGP
  1539.  * to defeat this feature, and redistributed their hotwired version to
  1540.  * others.  That creates problems for me (PRZ).
  1541.  *
  1542.  * Here is the problem.  Before I added this feature, there were maimed
  1543.  * versions of the PGP distribution package floating around that lacked
  1544.  * the manual.  One of them was uploaded to Compuserve, and was
  1545.  * distributed to countless users who called me on the phone to ask me why
  1546.  * such a complicated program had no manual.  It spread out to BBS systems
  1547.  * around the country.  And a freeware distributor got hold of the package
  1548.  * from Compuserve and enshrined it on CD-ROM, distributing thousands of
  1549.  * copies without the manual.  What a mess.
  1550.  *
  1551.  * Please don't make my life harder by modifying PGP to disable this
  1552.  * feature so that others may redistribute PGP without the manual.  If you
  1553.  * run PGP on a palmtop with no memory for the manual, is it too much to
  1554.  * ask that you type one little extra word on the command line to do a key
  1555.  * generation, a command that is seldom used by people who already know
  1556.  * how to use PGP?  If you can't stand even this trivial inconvenience,
  1557.  * can you suggest a better method of reducing PGP's distribution without
  1558.  * the manual?
  1559.  */
  1560.  
  1561. static unsigned ext_missing(char *prefix)
  1562. {
  1563.     static char const *const extensions[] =
  1564. #ifdef VMS
  1565.     { ".doc", ".txt", ".man", ".tex", ".", 0 };
  1566. /* gjm:
  1567.  * The stupid RISC OS filename conventions strike again.
  1568.  */
  1569. #elif defined(RISC_OS)
  1570.         { "/d", "/txt", "/doc", "/txt", "", 0 };
  1571. #else
  1572.     { ".doc", ".txt", ".man", ".tex", "", 0 };
  1573. #endif
  1574.     char const *const *p;
  1575.     char *end = prefix + strlen(prefix);
  1576.  
  1577.     for (p = extensions; *p; p++) {
  1578.     strcpy(end, *p);
  1579. #if 0                /* Debugging code */
  1580.     fprintf(pgpout, "Looking for \"%s\"\n", prefix);
  1581. #endif
  1582.     if (file_exists(prefix))
  1583.         return 0;
  1584.     }
  1585.     return 1;
  1586. }
  1587.  
  1588. /*
  1589.  * Returns mask of files missing
  1590.  */
  1591. static unsigned files_missing(char *prefix)
  1592. {
  1593. /* This changed to incorporate the changes in the Documentation subdirectory */
  1594. #ifdef MACTC5
  1595.     static char const *const names[] =
  1596.     {"Volume I", "Volume II", 0};
  1597. #else
  1598.     static char const *const names[] =
  1599.     {"pgpdoc1", "pgpdoc2", 0};
  1600. #endif
  1601.     char const *const *p;
  1602.     unsigned bit, mask = 3;
  1603.     int len = strlen(prefix);
  1604.  
  1605. #ifndef MACTC5
  1606.     /* Cannot do this on the macintosh because file_exists returns false on
  1607.        directories */
  1608. #ifndef VMS
  1609.     /*
  1610.      * Optimization: if directory doesn't exist, stop.  But access()
  1611.      * (used internally by file_exists()) doesn't work on dirs under VMS.
  1612.      */
  1613.     if (prefix[0] && !file_exists(prefix))    /* Directory doesn't exist? */
  1614.     return mask;
  1615. #endif /* VMS */
  1616. #endif /* MACTC5 */
  1617.     if (len && strchr(DIRSEPS, prefix[len - 1]) == 0)
  1618.     prefix[len++] = DIRSEPS[0];
  1619.     for (p = names, bit = 1; *p; p++, bit <<= 1) {
  1620.     strcpy(prefix + len, *p);
  1621.     if (!ext_missing(prefix))
  1622.         mask &= ~bit;
  1623.     }
  1624.  
  1625.     return mask;        /* Bitmask of which files exist */
  1626. }
  1627.  
  1628. /*
  1629.  * Search prefix directory and doc subdirectory.
  1630.  */
  1631. static unsigned doc_missing(char *prefix)
  1632. {
  1633.     unsigned mask;
  1634.     int len = strlen(prefix);
  1635.  
  1636.     mask = files_missing(prefix);
  1637.     if (!mask)
  1638.     return 0;
  1639. #if defined(VMS)
  1640.     if (len && prefix[len - 1] == ']') {
  1641.     strcpy(prefix + len - 1, ".doc]");
  1642.     } else {
  1643.     assert(!len || prefix[len - 1] == ':');
  1644.     strcpy(prefix + len, "[doc]");
  1645.     }
  1646. #elif defined(MACTC5)
  1647.     /* on the macintosh we must look for the documents in
  1648.        Documentation:PGP User's Guide: folder  */
  1649.     if (len && prefix[len - 1] != DIRSEPS[0])
  1650.         prefix[len++] = DIRSEPS[0];
  1651.     strcpy(prefix + len, "Documentation");
  1652.     len = strlen(prefix);
  1653.     mask &= files_missing(prefix);
  1654.     if (!mask)
  1655.         return 0;
  1656.     if (len && prefix[len - 1] != DIRSEPS[0])
  1657.         prefix[len++] = DIRSEPS[0];
  1658.     strcpy(prefix + len, "PGP User's Guide");
  1659.     mask &= files_missing(prefix);
  1660.     if (!mask)
  1661.         return 0;
  1662. #else
  1663.     if (len && prefix[len - 1] != DIRSEPS[0])
  1664.     prefix[len++] = DIRSEPS[0];
  1665.     strcpy(prefix + len, "doc");
  1666. #endif
  1667.  
  1668.     mask &= files_missing(prefix);
  1669.  
  1670.     prefix[len] = '\0';
  1671.     return mask;
  1672. }
  1673.  
  1674. /*
  1675.  * Expands a leading environment variable.  Returns 0 on success;
  1676.  * <0 if there is an error.
  1677.  */
  1678. static int expand_env(char const *src, char *dest)
  1679. {
  1680.     char const *var, *suffix;
  1681.     unsigned len;
  1682.  
  1683. /* gjm:
  1684.  * Under RISC OS, pathnames often start with $ with no
  1685.  * environment vars being involved: $ is the root directory.
  1686.  * So things starting $. aren't taken to be variable refs.
  1687.  */
  1688. #ifdef RISC_OS
  1689.     if (*src != '$' || src[1] == '.') {
  1690. #else
  1691.     if (*src != '$') {
  1692. #endif
  1693.     strcpy(dest, src);
  1694.     return 0;
  1695.     }
  1696.     /* Find end of variable */
  1697.     if (src[1] == '{') {    /* ${FOO} form */
  1698.     var = src + 2;
  1699.     len = strchr(var, '}') - (char*) var;
  1700.     suffix = src + 2 + len + 1;
  1701.     } else {            /* $FOO form - allow $ for VMS */
  1702.     var = src + 1;
  1703.     len = strspn(var, "ABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
  1704.     suffix = src + 1 + len;
  1705.     }
  1706.  
  1707.     memcpy(dest, var, len);    /* Copy name */
  1708.     dest[len] = '\0';        /* Null-terminate */
  1709.  
  1710.     var = getenv(dest);
  1711.     if (!var || !*var)
  1712.     return -1;        /* No env variable */
  1713.  
  1714.     /* Copy expanded form to destination */
  1715.     strcpy(dest, var);
  1716.  
  1717.     /* Add tail */
  1718.     strcat(dest, suffix);
  1719.  
  1720.     return 0;
  1721. }
  1722.  
  1723. /* Don't forget to change 'pgp26' whenever you update rel_version past 2.6 */
  1724. char const *const manual_dirs[] =
  1725. {
  1726. #if defined(VMS)
  1727.     "$PGPPATH", "", "[pgp]", "[pgp26]", "[pgp263]",
  1728.     PGP_SYSTEM_DIR, "SYS$LOGIN:", "SYS$LOGIN:[pgp]",
  1729.     "SYS$LOGIN:[pgp26]", "SYS$LOGIN:[pgp263]", "[-]",
  1730. #elif defined(UNIX)
  1731.     "$PGPPATH", "", "pgp", "pgp26", "pgp263", PGP_SYSTEM_DIR,
  1732.     "$HOME/.pgp", "$HOME", "$HOME/pgp", "$HOME/pgp26", "..",
  1733. #elif defined(AMIGA)
  1734.     "$PGPPATH", "", "pgp", "pgp26", ":pgp", ":pgp26", ":pgp263",
  1735.     ":", "/",
  1736. /* gjm:
  1737.  * RISC OS manual locations. The list is longer than it was in 2.6.2i .
  1738.  */
  1739. #elif defined(RISC_OS)
  1740.     "$PGPPATH", "", "pgp", "pgp26", "$.pgp", "$.pgp26", "$.pgp263",
  1741.     "$.pgp263i", "pgp:", "pgp26:", "pgp263:", "pgp263i:",
  1742.     LIBDIR, "$", "^",
  1743. #else                /* MSDOS or ATARI */
  1744.     "$PGPPATH", "", "pgp", "pgp26", "\\pgp", "\\pgp26", "\\pgp263",
  1745.     "\\", "..", "c:\\pgp", "c:\\pgp26",
  1746. #endif
  1747.     0};
  1748.  
  1749. #ifdef MACTC5
  1750. extern char appPathName[];
  1751. #endif
  1752.  
  1753. unsigned manuals_missing(void)
  1754. {
  1755.     char buf[256];
  1756.     unsigned mask = ~((unsigned)0);
  1757.     char const *const *p;
  1758.  
  1759. #ifdef MACTC5
  1760.     strcpy(buf, appPathName);
  1761.     mask &= doc_missing(buf);
  1762.     return mask;
  1763. #endif /* MACTC5 */
  1764.     for (p = manual_dirs; *p; p++) {
  1765.     if (expand_env(*p, buf) < 0)
  1766.         continue;        /* Ignore */
  1767.     mask &= doc_missing(buf);
  1768.     if (!mask)
  1769.         break;
  1770.     }
  1771.  
  1772.     return mask;
  1773. }
  1774.  
  1775. /*
  1776.  * Why all this code?
  1777.  *
  1778.  * See block of comments above.
  1779.  */
  1780.